package study.lang;

import java.math.BigDecimal;
import java.text.NumberFormat;

public class CalUtil {
    /**
     * @param dividend : 被除数(分子)
     * @param divisor  : 除数(分母)
     * @param digit    : 保留几位小数
     * @return String
     * @Description: 整数相除结果转换成指定位数的百分数
     */
    public static String getPercent(int dividend, int divisor, int digit) {
        Float result = (float) dividend / (float) divisor;
        if (result.isNaN()) {
            return "--";
        } else {
            //获取格式化对象
            NumberFormat nt = NumberFormat.getPercentInstance();
            //设置百分数精确度,即保留几位小数
            nt.setMinimumFractionDigits(digit);
            return nt.format(result);
        }
    }

    /**
     * 两个数相加
     *
     * @param str1
     * @param str2
     * @return
     */
    public static String add(String str1, String str2) {
        try {
            return new BigDecimal(str1).add(new BigDecimal(str2)).toPlainString();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 两个数相乘
     *
     * @param str1
     * @param str2
     * @return
     */
    public static String multiply(String str1, String str2) {
        try {
            return new BigDecimal(str1).multiply(new BigDecimal(str2)).toPlainString();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 两个数相减
     *
     * @param str1
     * @param str2
     * @return
     */
    public static String subtract(String str1, String str2) {
        try {
            return new BigDecimal(str1).subtract(new BigDecimal(str2)).toPlainString();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 两个数相除
     *
     * @param str1
     * @param str2
     * @return
     */
    public static String divide(String str1, String str2) {
        try {
            return new BigDecimal(str1).divide(new BigDecimal(str2), BigDecimal.ROUND_HALF_DOWN).toPlainString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 四舍六入五五成双
     *
     * @param str
     */
    public static String FourUpSixInto(String str) {
        BigDecimal b1 = new BigDecimal(str);
        BigDecimal b2 = b1.setScale(2, BigDecimal.ROUND_HALF_EVEN);
//        BigDecimal b2 = b1.setScale(2, BigDecimal.ROUND_HALF_UP);
        return b2.toString();
    }

    public static String fix(String str) {
        return new BigDecimal(str).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
    }

    public static String fix(String str, int roundMode) {
        return new BigDecimal(str).setScale(2, roundMode).toString();
    }

    public static void main(String[] args) {
        String jine = "255.00";
        String feilv = "0.003";
        String shouxufei = CalUtil.FourUpSixInto(CalUtil.multiply(jine, feilv));
        String realMoney = CalUtil.subtract(jine, shouxufei);
        System.out.println("交易金额[" + jine + "]\n费率[" + feilv + "]\n手续费[" + shouxufei + "]\n到帐金额[" + realMoney + "]\n");

        for (int i = 0; i < 20; i++) {
            double a = Math.random();
            double b = Math.random();
            System.out.printf("%.6f\t%.6f\t%.6f\t%s\n", a, b, a / b, fix(divide(a + "", b + "")));
        }
    }

}
